package com.jqbd.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wxh
 * @date 2022/10/10 11:56
 */

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String origin = request.getHeader("Origin");
        // 允许的跨域
        response.setHeader("Access-Control-Allow-Origin",origin);
        // 允许携带Cookie
        response.setHeader("Access-Control-Allow-Credentials","true");
        // 允许的请求头  预检请求需要这个设置
        response.setHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,Access-Token,token");
        response.setHeader("Access-Control-Expose-Headers", "*");//响应客户端的头部 允许携带Token 等等
        response.setHeader("Access-Control-Max-Age", "3600");   // 预检请求的结果缓存时间
        if (request.getMethod().equals("OPTIONS")){
            return false;
        }
        return true;
    }

}
